package com.example.hello.controller;

import com.example.hello.model.AvatarUploadResponse;
import com.example.hello.model.ErrorResponse;
import com.example.hello.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;

@RestController
@RequestMapping("/upLoad")
public class FileUploadController {

    @Autowired
    private FileUploadService fileUploadService;
    
    @PostMapping("/{empId}")
    public ResponseEntity<?> uploadAvatar(@RequestBody MultipartFile file, @PathVariable Integer empId) {
        try {
            // 上传头像
            AvatarUploadResponse response = fileUploadService.uploadAvatar(file, empId);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            ErrorResponse errorResponse = ErrorResponse.builder()
                    .code("INVALID_FILE_TYPE")
                    .message(e.getMessage())
                    .suggestions(Arrays.asList("转换图片格式", "使用推荐尺寸"))
                    .build();
            return ResponseEntity.badRequest().body(errorResponse);
        }
    }
}